{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-11-17T09:26:10.823570Z",
     "start_time": "2025-11-17T09:26:10.104079Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "\n",
    "data_ser = pd.Series([12,56,89,99,31])\n",
    "data_ser"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    12\n",
       "1    56\n",
       "2    89\n",
       "3    99\n",
       "4    31\n",
       "dtype: int64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-17T09:25:55.755407700Z",
     "start_time": "2025-11-17T08:28:26.750063Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from pandas._libs.tslibs.offsets import *\n",
    "\n",
    "data_offset = Week(2) + Hour(10)\n",
    "pd.date_range('2023/1/1','2023/1/31',freq = data_offset)\n"
   ],
   "id": "98af6efb6969bdb9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2023-01-01 00:00:00', '2023-01-15 10:00:00',\n",
       "               '2023-01-29 20:00:00'],\n",
       "              dtype='datetime64[ns]', freq='346h')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-17T09:25:55.771963200Z",
     "start_time": "2025-11-17T08:34:44.679778Z"
    }
   },
   "cell_type": "code",
   "source": [
    "period_index = pd.period_range('2023/1/8','2023/8/31',freq = 'M')\n",
    "per_ser = pd.Series(np.arange(8)+1,period_index)\n",
    "per_ser"
   ],
   "id": "8adac73d9c8ce10e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2023-01    1\n",
       "2023-02    2\n",
       "2023-03    3\n",
       "2023-04    4\n",
       "2023-05    5\n",
       "2023-06    6\n",
       "2023-07    7\n",
       "2023-08    8\n",
       "Freq: M, dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-17T09:25:55.773963900Z",
     "start_time": "2025-11-17T08:33:46.516662Z"
    }
   },
   "cell_type": "code",
   "source": [
    "str_list = ['2021','2022','2023']\n",
    "pd.PeriodIndex(str_list,freq='Y-DEC')"
   ],
   "id": "b15fa5a3deabbdbe",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2021', '2022', '2023'], dtype='period[Y-DEC]')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-17T09:25:55.774963200Z",
     "start_time": "2025-11-17T08:39:34.038001Z"
    }
   },
   "cell_type": "code",
   "source": [
    "period = pd.Period('2022',freq='Y-DEC')\n",
    "period.asfreq(freq='M',how='end')"
   ],
   "id": "42c65dc8ab750883",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Period('2022-12', 'M')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-17T09:25:55.778963500Z",
     "start_time": "2025-11-17T09:02:45.008127Z"
    }
   },
   "cell_type": "code",
   "source": [
    "temp = pd.period_range('2024.1',periods=30)\n",
    "temp_ser = pd.Series(np.arange(30)+1,temp)\n"
   ],
   "id": "b2c2b87037b36ceb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2024-01-01     1\n",
       "2024-01-02     2\n",
       "2024-01-03     3\n",
       "2024-01-04     4\n",
       "2024-01-05     5\n",
       "2024-01-06     6\n",
       "2024-01-07     7\n",
       "2024-01-08     8\n",
       "2024-01-09     9\n",
       "2024-01-10    10\n",
       "2024-01-11    11\n",
       "2024-01-12    12\n",
       "2024-01-13    13\n",
       "2024-01-14    14\n",
       "2024-01-15    15\n",
       "2024-01-16    16\n",
       "2024-01-17    17\n",
       "2024-01-18    18\n",
       "2024-01-19    19\n",
       "2024-01-20    20\n",
       "2024-01-21    21\n",
       "2024-01-22    22\n",
       "2024-01-23    23\n",
       "2024-01-24    24\n",
       "2024-01-25    25\n",
       "2024-01-26    26\n",
       "2024-01-27    27\n",
       "2024-01-28    28\n",
       "2024-01-29    29\n",
       "2024-01-30    30\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-17T09:26:14.124964Z",
     "start_time": "2025-11-17T09:26:13.944124Z"
    }
   },
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "               OFFENSE_TYPE_ID OFFENSE_CATEGORY_ID       REPORTED_DATE  \\\n",
       "0             traffic-accident    traffic-accident 2022-05-22 14:41:00   \n",
       "1            threats-to-injure     public-disorder 2022-05-17 20:35:00   \n",
       "2  burglary-residence-by-force            burglary 2022-06-07 07:47:00   \n",
       "3                  theft-other             larceny 2022-05-26 16:46:00   \n",
       "4         criminal-trespassing    all-other-crimes 2022-06-07 07:42:00   \n",
       "\n",
       "      GEO_LON    GEO_LAT  IS_CRIME  IS_TRAFFIC  \n",
       "0 -104.673812  39.849292         0           1  \n",
       "1 -105.020053  39.694351         1           0  \n",
       "2 -104.981677  39.763597         1           0  \n",
       "3 -104.839119  39.769694         1           0  \n",
       "4 -104.673812  39.849292         1           0  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>OFFENSE_TYPE_ID</th>\n",
       "      <th>OFFENSE_CATEGORY_ID</th>\n",
       "      <th>REPORTED_DATE</th>\n",
       "      <th>GEO_LON</th>\n",
       "      <th>GEO_LAT</th>\n",
       "      <th>IS_CRIME</th>\n",
       "      <th>IS_TRAFFIC</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>traffic-accident</td>\n",
       "      <td>traffic-accident</td>\n",
       "      <td>2022-05-22 14:41:00</td>\n",
       "      <td>-104.673812</td>\n",
       "      <td>39.849292</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>threats-to-injure</td>\n",
       "      <td>public-disorder</td>\n",
       "      <td>2022-05-17 20:35:00</td>\n",
       "      <td>-105.020053</td>\n",
       "      <td>39.694351</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>burglary-residence-by-force</td>\n",
       "      <td>burglary</td>\n",
       "      <td>2022-06-07 07:47:00</td>\n",
       "      <td>-104.981677</td>\n",
       "      <td>39.763597</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>theft-other</td>\n",
       "      <td>larceny</td>\n",
       "      <td>2022-05-26 16:46:00</td>\n",
       "      <td>-104.839119</td>\n",
       "      <td>39.769694</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>criminal-trespassing</td>\n",
       "      <td>all-other-crimes</td>\n",
       "      <td>2022-06-07 07:42:00</td>\n",
       "      <td>-104.673812</td>\n",
       "      <td>39.849292</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3,
   "source": [
    "alarm_record = pd.read_csv('./alarm.csv',parse_dates=['REPORTED_DATE'])\n",
    "alarm_record.head()"
   ],
   "id": "a72ca50e10cbb0be"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-17T09:31:42.816969Z",
     "start_time": "2025-11-17T09:31:42.759846Z"
    }
   },
   "cell_type": "code",
   "source": "alarm_record.info()",
   "id": "58d21c7f975de8f3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 65617 entries, 0 to 65616\n",
      "Data columns (total 7 columns):\n",
      " #   Column               Non-Null Count  Dtype         \n",
      "---  ------               --------------  -----         \n",
      " 0   OFFENSE_TYPE_ID      65617 non-null  object        \n",
      " 1   OFFENSE_CATEGORY_ID  65617 non-null  object        \n",
      " 2   REPORTED_DATE        65617 non-null  datetime64[ns]\n",
      " 3   GEO_LON              65617 non-null  float64       \n",
      " 4   GEO_LAT              65617 non-null  float64       \n",
      " 5   IS_CRIME             65617 non-null  int64         \n",
      " 6   IS_TRAFFIC           65617 non-null  int64         \n",
      "dtypes: datetime64[ns](1), float64(2), int64(2), object(2)\n",
      "memory usage: 3.5+ MB\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-17T09:34:56.080259Z",
     "start_time": "2025-11-17T09:34:56.070943Z"
    }
   },
   "cell_type": "code",
   "source": "final_alarm_record = alarm_record.sort_index()",
   "id": "2a887442967b0087",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-17T09:35:55.730064Z",
     "start_time": "2025-11-17T09:35:55.704668Z"
    }
   },
   "cell_type": "code",
   "source": "alarm_record['REPORTED_DATE'].dt.date.head",
   "id": "61f6ffbdc27e4a88",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method NDFrame.head of 0        2022-05-22\n",
       "1        2022-05-17\n",
       "2        2022-06-07\n",
       "3        2022-05-26\n",
       "4        2022-06-07\n",
       "            ...    \n",
       "65612    2022-09-13\n",
       "65613    2022-09-12\n",
       "65614    2022-09-12\n",
       "65615    2022-09-12\n",
       "65616    2022-09-12\n",
       "Name: REPORTED_DATE, Length: 65617, dtype: object>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-17T09:35:58.519516Z",
     "start_time": "2025-11-17T09:35:58.474120Z"
    }
   },
   "cell_type": "code",
   "source": "record_3_5 = final_alarm_record.sort_index().loc['2022-03-01':'2022-05-31']",
   "id": "e34cb7cdcf143b43",
   "outputs": [],
   "execution_count": 10
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
